﻿using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using Jinndev.UI;
using B83.Win32;
using System.IO;

namespace Jinndev {

    public class CopyFileUI : BaseUIBehaviour {

        #region static
        public static readonly string PATH = "Prefab/UI/CopyFile/CopyFileUI";

        public static CopyFileUI Create() {
            return UIManager.Instance.Add<CopyFileUI>(PATH, UIParam.FullScreen);
        }
        #endregion

        public InputField srcDirInput;
        public InputField destDirInput;
        public Button btnStart;
        public RectTransform logArea;

        private LogView logView;

        private void Awake() {
            Util.InitBrowsePasteDirectory(srcDirInput, "选择源文件夹");
            Util.InitBrowsePasteDirectory(destDirInput, "选择目标文件夹");

            srcDirInput.onValueChanged.AddListener(OnInputChange);
            destDirInput.onValueChanged.AddListener(OnInputChange);

            //UnityDragAndDropHook.InstallHook();
            //UnityDragAndDropHook.OnDroppedFiles += OnDroppedFiles;

            logView = LogView.Create(logArea);
        }

        private void OnDestroy() {
            //UnityDragAndDropHook.OnDroppedFiles -= OnDroppedFiles;
            //UnityDragAndDropHook.UninstallHook();
        }

        private void OnInputChange(string value) {

        }

        public void OnClickStart() {
            StartCoroutine(Procesing());
        }

        private IEnumerator Procesing() {
            logView.Clear();

            string srcDir = srcDirInput.text.Trim();
            string destDir = destDirInput.text.Trim();
            if (!Directory.Exists(srcDir)) {
                logView.LogError("源文件夹不存在");
                yield break;
            }
            if (!Directory.Exists(destDir)) {
                logView.LogError("目标文件夹不存在");
                yield break;
            }

            string[] srcFiles = Directory.GetFiles(srcDir, "*", SearchOption.AllDirectories);
            yield return new WaitForEndOfFrame();
            string[] destFiles = Directory.GetFiles(destDir, "*", SearchOption.AllDirectories);
            yield return new WaitForEndOfFrame();

            Dictionary<string, string> destFileDict = new Dictionary<string, string>();
            foreach (string destFile in destFiles) {
                destFileDict[Path.GetFileName(destFile)] = destFile;
            }

            Texture2D tex = new Texture2D(0, 0);

            for (int i = 0; i < srcFiles.Length; i++) {
                string srcFile = srcFiles[i]; // d:/123/456/def.png
                string fileName = Path.GetFileName(srcFile); // def.png
                if (fileName.EndsWith(".meta")) {
                    continue;
                }
                string subDir = srcFile.Substring(srcDir.Length);
                subDir = subDir.Substring(0, subDir.Length - fileName.Length);

                if (!destFileDict.TryGetValue(fileName, out string destFile)) {
                    logView.LogError($"目标文件不存在: {Path.Combine(subDir, fileName)}");
                    continue;
                }

                tex.LoadImage(File.ReadAllBytes(srcFile));
                int width = tex.width;
                int height = tex.height;

                tex.LoadImage(File.ReadAllBytes(destFile));
                string sizeChanged = (width != tex.width || height != tex.height) ? $" <color=#dd6666>[尺寸变化] {tex.width}x{tex.height}=>{width}x{height}</color>" : string.Empty;

                // 备份源文件
                string backupFile = srcDir + "_backup" + srcFile.Substring(srcDir.Length);
                if (!File.Exists(backupFile)) {
                    CommonUtil.PrepareFileDir(backupFile);
                    File.Copy(destFile, backupFile, false);
                }

                // 复制到目标文件
                File.Copy(srcFile, destFile, true);
                logView.Log($"复制文件: {Path.Combine(subDir, fileName)}{sizeChanged}");

                yield return new WaitForEndOfFrame();
            }
            logView.Log("完成");
        }

    }

}
